/*
 *  Player.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 9/21/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "Player.h"
#include "SpaceTrader.h"
#include <iostream>
/* Player Constructor */

Player::Player() : credits(1000), curr_ship(new Ship(1,1,1,1,"def_ship2")), name(""), icon(NULL), itemLimit(30) { }
Player::Player(const Player &P) : credits(P.credits), curr_ship(P.curr_ship), name(P.name), icon(P.icon), itemLimit(P.itemLimit)
{
    curr_ship = new Ship(1,1,1,1,"def_ship2");
    for (int i = 0; i < 4; i++){
        stats[i] = P.stats[i];
    }
}
Player::~Player()
{
	free(curr_ship);
}
Player& Player::operator=(const Player &P){
    if (&P != this){
        delete icon;
        delete curr_ship;
        for (int i = 0; i < 4; i++){
            stats[i] = P.stats[i];
        }
        credits = P.credits;
        curr_ship = P.curr_ship;
        name = P.name;
        icon = P.icon;
        itemLimit = P.itemLimit;
    }
    return *this;
}

void Player::setName(std::string newName)
{
	name = newName;
}

std::string Player::getName()
{
	return name;
}

void Player::setStats(int newStats[4])
{
	for (int i = 0; i < 4; i++){
        stats[i] = newStats[i];
    }
}

int* Player::getStats()
{
	return (int *)stats;
}

void Player::setShip(Ship *newShip)
{
	curr_ship = newShip;
}

int Player::getCredits()
{
	return credits;
}

void Player::setCredits(int c){
    credits = c;
}

Ship *Player::getShip()
{
	return curr_ship;
}
/*
 *	Player's sell function inherited from merchant
 *	Eventually will take in a trade good to replace good and price.
 */
bool Player::sell(int good,int price, int qty)
{
    bool ret = false;
	if(inventory[good]>=qty) {
		inventory[good] -= qty;
		itemCount -= qty;
		credits += price*qty;
		ret = true;
	}
	return ret;
}

/*
 *	Player's buy function inherited from merchant
 *	Eventually will take in a trade good to replace good and price.
 */
bool Player::buy(int good,int price, int qty)
{
    bool ret = false;
	//std::cout << price << std::endl;
	if(credits-(price*qty)>=0 && (itemCount + qty) <= itemLimit) {
		inventory[good] += qty;
		itemCount += qty;
		credits -= price*qty;
		ret = true;
	}
	return ret;
}
/*
 *	Blits players image to screen.
 */
void Player::show(SDL_Surface *screen) 
{
	curr_ship->show(screen);
}

/*
 *	REM:Debug function 
 */
int *Player::retInvArray()
{
	return inventory;
}

int Player::getFuel() {
	return curr_ship->getFuel();
}

void Player::travel(int distance){
    curr_ship->setFuel(curr_ship->getFuel()-distance); //multiply distance by MPG
}

int Player::buyFuel(int qty, double price){
    if (credits >= price*qty){
        if (qty + getFuel() <= getShip()->getCapacity()){
            credits -= price*qty;
            curr_ship->setFuel(getFuel() + qty);
            return 0;
        } else {
            if (getShip()->getCapacity()-getFuel() > 0){
                credits -= price*(getShip()->getCapacity()-getFuel());
                curr_ship->setFuel(getShip()->getCapacity());
                return 0;
            } else {
                return 2;
            }
        }
    } else {
        return 1;
    }
}
